﻿/// <reference path="../../Scripts/jquery-1.4.1-vsdoc.js" />

//页面传值到此
var product = {};
//id
//materials: [id, barcode, amount, amountNeed]

$(function () {
	$("#btnPrint").hide();
	$("#btnRefresh").hide();

	$.each(product.materials, function (i, m) {
		m.amount = 0;
	});

	//this method will be invoked after barcode is input.
	addBarcodeReadyHandler(function (barcode) {
		$("#warning").hide();

		var m = $.grep(product.materials, function (m) {
			return m.barcode == barcode;
		});

		if (m.length == 0) {
			alert("产品中不需要此物料！");
			return;
		}

		var material = m[0];
		if (material.amountNeed == material.amount) {
			alert("该物料已经超过所需要的数目！");
			$("#amount_" + material.id).effect("pulsate")
			return;
		}

		material.amount++;
		$("#amount_" + material.id).html(material.amount).effect("pulsate")
	});
});

function submitAllData() {
	var materialsNeed = product.materials;
	for (var i = 0; i < materialsNeed.length; i++) {
		var m = materialsNeed[i];
		if (m.amount < m.amountNeed) {
			alert("物料 " + m.barcode + " 的数量不够，提交失败！");
			return;
		}
	}

	$("#btnSubmit").hide();
	$.post(actionUrl("Assemble", "AJAX_SubmitProductAssemble"), { productId: product.id }, function (res) {
		alert(res.Message);
		if (res.Success) {
			$("#btnPrint").show();
			$("#btnRefresh").show();
		}
		else {
			$("#btnSubmit").show();
		}
	});
}

function printProduct() {
	window.print();
}

function startNewProduct() {
	window.location.reload();
}